package com.zw.stock_sell_storage.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.stock_sell_storage.dto.MyPageDto;
import com.zw.stock_sell_storage.dto.SupplierDto;
import com.zw.stock_sell_storage.service.ISupplierService;
import com.zw.stock_sell_storage.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    ISupplierService iSupplierService;

    @RequestMapping("/querySupplierList")
    public Page<SupplierDto> querySupplierList(@RequestBody MyPageDto<SupplierDto> myPageDto){
        return iSupplierService.querySupplierList(myPageDto);
    }

    @RequestMapping("/querySupplierOne")
    public SupplierDto querySupplierOne(int supplierId){
        return iSupplierService.querySupplierOne(supplierId);
    }

    @RequestMapping("/updateSupplier")
    public boolean updateSupplier(@RequestBody SupplierDto supplierDto){
        return iSupplierService.updateSupplier(supplierDto);
    }

    @RequestMapping("/addSupplier")
    public boolean addSupplier(@RequestBody SupplierDto supplierDto){
        return iSupplierService.addSupplier(supplierDto);
    }

    @RequestMapping("/importSupplierList")
    public boolean importSupplierList(@RequestParam("file") MultipartFile file) throws IOException {

        //@RequestBody List<SupplierDto> supplierList
        // 解析数据
        byte[] bytes = file.getBytes();
        InputStream fileInputStream = new ByteArrayInputStream(bytes);
        List<LinkedHashMap<String, Object>> maps = FileUtils.xlsxImportExcel(fileInputStream, 0, 1, 0);
        System.out.println(JSON.toJSONString(maps));
        //添加进供应商集合
        List<SupplierDto> supplierList = new ArrayList<>();
        for (LinkedHashMap<String, Object> map : maps) {
            SupplierDto supplierDto = new SupplierDto();
            supplierDto.setSupplierName(map.get("supplierName")+"");
            supplierDto.setSupplierResponsiblePersonName(map.get("supplierResponsiblePersonName")+"");
            supplierDto.setSupplierResponsiblePersonPhone(new BigDecimal(map.get("supplierResponsiblePersonPhone")+"").toPlainString());
            supplierDto.setSupplierAddress(map.get("supplierAddress")+"");
            supplierDto.setSupplierBusinessScope(map.get("supplierBusinessScope")+"");
            supplierList.add(supplierDto);
        }
        return iSupplierService.importSupplierList(supplierList);
    }

    @RequestMapping("/queryAllSupplierList")
    public List<SupplierDto> queryAllSupplierList(){
        return iSupplierService.queryAllSupplierList();
    }

}
